<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta charset="utf-8">
<title>Mua~亲亲宠管理平台·新增管理员</title>

<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/bootstrapValidator.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrapValidator.js"></script>
</head>

<body>
	<%@ include file="/WEB-INF/jsp/mgnt/mgntTop.jspf"%>
	<div class="container">
		<div class="panel panel-info">
			<div class="panel-heading">
				<h3 class="panel-title">新增管理员</h3>
			</div>
			<div class="panel-body">
				<div class="col-sm-offset-3  col-md-9 column">
					<form id="addForm" class="form-horizontal">
						<div class="form-group">
							<label class="col-sm-2 control-label">登录名</label>
							<div class="col-sm-5">
								<input type="text" name="loginname" class="form-control" />
							</div>
						</div>
						<div class="col-sm-offset-2 col-sm-3">
							<button type="button" class="btn btn-info"
								onclick="checkAndSubmit()">确认信息</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {
			$('#addForm').bootstrapValidator({
				message : 'This value is not valid',
				feedbackIcons : {
					valid : 'glyphicon glyphicon-ok',
					invalid : 'glyphicon glyphicon-remove',
					validating : 'glyphicon glyphicon-refresh'
				},
				fields : {
					loginname : {
						message : '',
						validators : {
							notEmpty : {
								message : '登录名不能为空'
							},
							stringLength : {
								min : 4,
								max : 20,
								message : '登录名最少4位，最多20位'
							},
							regexp : {
								regexp : /^[a-zA-Z][a-zA-Z0-9_]+$/,
								message : '用户名只能由字母，数字，下划线组成，并且只能由字母开头'
							},
							remote : {
								url : 'admin/checkSameLoginname.do',
								message : "该用户名已经存在！",
								delay : 500
							},
						}
					}
				}
			});
		});
		function checkAndSubmit() {
			$("#addForm").bootstrapValidator('validate');//提交验证  
			if ($("#addForm").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码  
				$.ajax({
					url : "admin/addAdmin.do",
					data : $("#addForm").serialize(),
					type : "POST",
					success : function(result) {
						//请求成功时
						if (result.msg == 'success') {

							$(".modal-body").html("新增管理员成功，点击确定前往管理员列表");
							$("#topModal").modal("show");
							$("#topModal").on('hidden.bs.modal', function() {
								var gotoURL = "admin/tolist.do";
								window.location.href = gotoURL;
							});
						} else if (result.msg == 'error') {
							$(".modal-body").html(result.errorMsg);
							$("#topModal").modal("show");
						}
					},
					error : function() {
						//请求失败时
						$(".modal-body").html("新增管理员");
						$("#topModal").modal("show");
					}

				});
			}
		}
	</script>
</body>
</html>
